package com.hup.timer.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import com.hup.utils.commons.io.BaseConfig;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hugan
 * @date 2020/1/9
 */
@Data
@JSONType(orders={"appPort","globalSettingConfig"})
public class TimerConfig implements BaseConfig<TimerConfig> {

    public int appPort = 38068;

    public TimeViewConfig timeViewConfig = new TimeViewConfig();

    public TimeWindowConfig timeWindowConfig = new TimeWindowConfig();

    /**
     * [拖拽,菜单等鼠标操作]要双击鼠标才触发:true-双击后触发,false-单击后触发
     */
    public Boolean actBy2Click = true;

    /**
     * 启用定时置顶守护线程功能
     */
    public Boolean autoOnTop = true;
    /**
     * 定时置顶的时间间隔(秒)
     */
    public Integer autoOnTopSpan = 5;

    /**
     * 启用[使窗口置顶]的全局热键:ctrl+shift+alt
     */
    public Boolean enableOnTopGlobalHotKey = true;

    /**
     * 鼠标穿透
     */
    public Boolean mousePenetration = false;

    @JSONField(serialize = false)
    private List<ConfigChangedListener> configChangedListeners = new ArrayList<>();

    @Override
    public boolean debug() {
        return false;
    }

    @Override
    public boolean afterLoadFromFileOrReset(TimerConfig fileConfig) {
        boolean needSave = false;
        if (fileConfig == null) {
            //为了保存AppPort(有可能在首次启动时,端口号被占用而启动失败,需要到配置文件中修改port)
            needSave = true;
        }
        if (timeWindowConfig.afterLoadFromFileOrReset(timeWindowConfig)) {
            needSave = true;
        }
        return needSave;
    }

    /**
     * @param listener 回调会在 {@link #fireConfigChanged()} 时触发
     */
    public void addConfigChangedListener(ConfigChangedListener listener) {
        configChangedListeners.add(listener);
    }

    public void fireConfigChanged() {
        for (ConfigChangedListener listener : configChangedListeners) {
            listener.afterConfigChanged(this);
        }
    }

    public interface ConfigChangedListener {
        void afterConfigChanged(TimerConfig config);
    }

}
